package com.zl.smartmall.library.vo;

import java.util.List;

import net.tsz.afinal.FinalDb;

import org.json.JSONException;

import android.content.Context;

import com.zl.smartmall.library.po.AdvertInfo;
import com.zl.smartmall.library.util.CrashHandler;

public class AdvertInfoDao {

	private FinalDb mFinalDb;
	
	public AdvertInfoDao(Context context){
		mFinalDb = FinalDb.create(context);
	}
	
	public void insert(List<AdvertInfo> models) {
		synchronized (AdvertInfoDao.class) {
			try {
				for(AdvertInfo model : models) {
					model.encode();
				}
				mFinalDb.batchSave(models);
				
			} catch (Exception e) {
				CrashHandler.e(e);
			}
		}
	}
	public void insert(AdvertInfo model){
		synchronized (AdvertInfoDao.class) {
			if(model == null){
				return;
			}
			try {
				model.encode();
				mFinalDb.save(model);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public void delete() {
		synchronized (AdvertInfoDao.class) {
			mFinalDb.deleteByWhere(AdvertInfo.class, null);
		}
	}
	
	public void delete(int affiliation) {
		synchronized (AdvertInfoDao.class) {
			mFinalDb.deleteByWhere(AdvertInfo.class, "affiliation=" + affiliation);
		}
	}
	
	public List<AdvertInfo> query() {
		synchronized (AdvertInfoDao.class) {
			List<AdvertInfo> models = mFinalDb.findAll(AdvertInfo.class);
			try {
				for(AdvertInfo model : models) {
					model.decode();
				}
				
			} catch (Exception e) {
				CrashHandler.e(e);
			}
			
			return models;
		}

	}
	
	public List<AdvertInfo> query(int affiliation) {
		synchronized (AdvertInfoDao.class) {
			List<AdvertInfo> models = mFinalDb.findAllByWhere(AdvertInfo.class, "affiliation=" + affiliation);
			try {
				for(AdvertInfo model : models) {
					model.decode();
				}
			} catch (Exception e) {
				CrashHandler.e(e);
			}
			return models;
		}
	}

}
